package zhihui.mobile.ui.express;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhihui.mobile.R;
import zhihui.mobile.bean.express.ExpressBeanListV2;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.express.ExpressManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.ui.BaseFragment;

import static zhihui.mobile.R.id.exproess_check_all_cb;

/**
 * Created by Administrator on 2017/9/16.
 */

public class ReceiveExpressFragment extends BaseFragment {
    @Bind(R.id.express_rv)
    RecyclerView expressRv;
    @Bind(exproess_check_all_cb)
    CheckBox exproessCheckAllCb;
    @Bind(R.id.exproess_all_num_tv)
    TextView exproessAllNumTv;

    private ReceiveExpressAdapter expressAdapter;

    @Override
    public void initView() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.exprees_receive_fragment;
    }

    @Override
    protected IPresenter[] getPresenters() {
        return new IPresenter[0];
    }

    @Override
    protected void onInitPresenter() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        exproessCheckAllCb.setChecked(false);
        exproessAllNumTv.setText("0");
        expressRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        expressRv.setHasFixedSize(true);
        ExpressManager.getInstance().initExpressList(new IHttpCallBackListener() {
            @Override
            public void success(JSONObject result) {
                Gson gson = new Gson();
                ExpressBeanListV2 expressBeanListV2 = gson.fromJson(result.toString(), ExpressBeanListV2.class);
                expressAdapter = new ReceiveExpressAdapter(expressBeanListV2.getData().getInner(), getActivity());
                expressRv.setAdapter(expressAdapter);
                expressAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public boolean onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        selectNum();
                        for (int i = 0; i < expressAdapter.getData().size(); i++) {
                            if (!expressAdapter.getData().get(i).isCheck() && expressAdapter.getData().get(i).getStatus_f() == 0 | (expressAdapter.getData().get(i).getStatus_f() == 4)) {
                                exproessCheckAllCb.setChecked(false);
                                return true;
                            }
                        }
                        exproessCheckAllCb.setChecked(true);
                        return false;
                    }
                });
                expressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        CheckBox checkBox = (CheckBox) expressAdapter.getViewByPosition(expressRv, position, R.id.item_check_cb);
                        checkBox.performClick();
                    }
                });
            }

            @Override
            public void failed(String message) {
                com.zgm.utils.UiUtils.ToastInCenter(getActivity(), message, Toast.LENGTH_SHORT);
            }
        }, UserManager.getInstance().getCurUser().getNo());

        exproessCheckAllCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (int i = 0; i < expressAdapter.getData().size(); i++) {
                        expressAdapter.getData().get(i).setCheck(true);
                        expressAdapter.notifyDataSetChanged();
                    }
                } else {
                    for (int i = 0; i < expressAdapter.getData().size(); i++) {
                        if (!expressAdapter.getData().get(i).isCheck()) {
                            return;
                        }
                    }
                    for (int i = 0; i < expressAdapter.getData().size(); i++) {
                        expressAdapter.getData().get(i).setCheck(false);
                        expressAdapter.notifyDataSetChanged();
                    }
                }
            }
        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);

    }

    @OnClick({exproess_check_all_cb, R.id.exproess_send_to_home_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case exproess_check_all_cb:
                selectNum();
                break;
            case R.id.exproess_send_to_home_tv:
                if (exproessAllNumTv.getText().toString().equals("0")) {
                    com.zgm.utils.UiUtils.ToastInCenter(getActivity(), "请选择包裹", Toast.LENGTH_SHORT);
                    return;
                }
                ArrayList<ExpressBeanListV2.DataBean.InnerBean> dataBeens = new ArrayList<>();
                for (int i = 0; i < expressAdapter.getData().size(); i++) {
                    if (expressAdapter.getData().get(i).isCheck() && (expressAdapter.getData().get(i).getStatus_f() == 0) | (expressAdapter.getData().get(i).getStatus_f() == 4)) {
                        dataBeens.add((expressAdapter.getData().get(i)));
                    }
                }
                Intent intent = new Intent();
                intent.setClass(getActivity(), ExpressOrderActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data", dataBeens);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    private void selectNum() {
        int allNum = 0;
        for (int i = 0; i < expressAdapter.getData().size(); i++) {
            if (expressAdapter.getData().get(i).isCheck() && (expressAdapter.getData().get(i).getStatus_f() == 0) | (expressAdapter.getData().get(i).getStatus_f() == 4)) {
                allNum++;
            }
        }
        exproessAllNumTv.setText("" + allNum);
    }
}
